//package dsk.demo.product;
//
//import cn.hutool.core.io.FileUtil;
//import cn.hutool.core.util.StrUtil;
//import cn.hutool.http.HttpUtil;
//import cn.hutool.json.JSONArray;
//import cn.hutool.json.JSONNull;
//import cn.hutool.json.JSONObject;
//import cn.hutool.json.JSONUtil;
//import cn.hutool.poi.excel.ExcelUtil;
//import cn.hutool.poi.excel.ExcelWriter;
//import dsk.demo.product.entity.ProductEntity;
//import dsk.demo.product.entity.ProductRefEntity;
//import dsk.demo.product.util.UrlUtil;
//import org.junit.jupiter.api.Test;
//
//import java.io.BufferedReader;
//import java.io.File;
//import java.io.IOException;
//import java.nio.charset.Charset;
//import java.util.*;
//
//public class 博士在线目录爬取 {
//
//    //查询单个产品 详情 url
//    private static String baseProductURL = "https://apiservice.bosch-trading.com.cn/ecat-service/api/part/car/web/query";
//
//    //查询目录URL
//    private static String baseCatalogURL = "https://apiservice.bosch-trading.com.cn/ecat-service/api/part/list";
//
//    private static ExcelWriter refWriter = ExcelUtil.getWriter("C:\\Users\\Administrator\\Desktop\\火花塞_OEM.xls");
//    private static ExcelWriter productWriter = ExcelUtil.getWriter("C:\\Users\\Administrator\\Desktop\\火花塞_产品.xls");
//
//    static {
//        //配置别名
//        LinkedHashMap<String, String> refMap = new LinkedHashMap<>();
//        refMap.put("productNo","本厂编号");
//        refMap.put("brand","品牌");
//        refMap.put("refNo","OEM号码");
//        refWriter.setHeaderAlias(refMap);
//        //只写入配置了别名的属性
//        refWriter.setOnlyAlias(true);
//
//        //配置别名
//        LinkedHashMap<String, String> map = new LinkedHashMap<>();
//        map.put("productNo","本厂编号");
//        map.put("image","图片地址");
//        map.put("price","价格");
//        map.put("attributes","属性");
//        productWriter.setHeaderAlias(map);
//        //只写入配置了别名的属性
//        productWriter.setOnlyAlias(true);
//    }
//    /**
//     * 爬取博士在线目录 产品ID
//     */
//    @Test
//    public void mulu() throws InterruptedException {
//        Integer totalPages = 1;
//        Integer pageIndex =1;
//        File detailFile = new File("C:\\Users\\Administrator\\Desktop\\火花塞_目录.txt");
//        while(true) {
//            if (pageIndex > totalPages) {
//                break;
//            }
//            //获取第一页请求地址
//            Map<String, String> params = new HashMap<>();
//            params.put("page",pageIndex + "");
//            params.put("pageSize","50");
//            params.put("categoryIds","50,49,48,52,53,51,");
//            params.put("partNo","");
//            params.put("carIds","");
//            params.put("isEffectiveMapping","1");
//            params.put("partOnSale","");
//            params.put("languageType","zh-cn");
//            String realUrl = UrlUtil.getRealUrl(baseCatalogURL, params);
//
//            String body = HttpUtil.createGet(realUrl)
//                    .header("Content-Type", "application/json")
//                    .execute().body();
//            Map map = JSONUtil.toBean(body, Map.class);
//            Map resultData = (Map)map.get("resultData");
//            totalPages = (Integer)((Map)resultData.get("pageInfo")).get("totalPages");
//            List<String> urlList = new ArrayList<>();
//            //解析逻辑,将所有的产品Url 保存起来.
//            for (Object item : (JSONArray) resultData.get("partList")) {
//                JSONObject product = (JSONObject) item;
//                String partId = (String) product.get("partId");
//                Map<String, String> productParams = new HashMap<>();
//                //carIds=&levelIds=&partIds=19
//                productParams.put("carIds","");
//                productParams.put("levelIds","");
//                productParams.put("partIds",partId);
//                String realPartUrl = UrlUtil.getRealUrl(baseProductURL, productParams);
//                System.out.println(realPartUrl);
//                urlList.add(realPartUrl);
//            }
//
//            FileUtil.appendLines(urlList,detailFile, Charset.forName("utf-8"));
//            pageIndex++;
//            Thread.sleep(3000);
//        }
//
//    }
//
//    /**
//     * 爬取博士在线目录 产品详情 OEM号码
//     */
//    @Test
//    public void productDetails () throws IOException, InterruptedException {
//        File detailFile = new File("C:\\Users\\Administrator\\Desktop\\火花塞_目录.txt");
//        BufferedReader reader = FileUtil.getReader(detailFile, Charset.forName("utf-8"));
//        String line;
//        while ( (line = reader.readLine()) != null) {
//            List<ProductEntity> resultList = new ArrayList<>();
//            String body = HttpUtil.createGet(line)
//                    .header("Content-Type", "application/json")
//                    .execute().body();
//            Map map = JSONUtil.toBean(body, Map.class);
//            Map resultData = (Map)map.get("resultData");
//            JSONArray partVOList = (JSONArray)resultData.get("partVOList");
//            JSONObject productDetail = (JSONObject) partVOList.get(0);
//            JSONObject partDTO = (JSONObject) productDetail.get("partDTO");
//            ProductEntity productEntity = new ProductEntity();
//            //产品编号
//            String partNo = (String) partDTO.get("partNo");
//            productEntity.setProductNo(partNo);
//            //产品属性 partFeatureValueList
//            JSONArray partFeatureValueList = (JSONArray) partDTO.get("partFeatureValueList");
//            StringBuilder sb = new StringBuilder();
//            for (Object paramObj : partFeatureValueList) {
//                JSONObject param = (JSONObject) paramObj;
//                Object featureNameCn = param.getOrDefault("featureNameCn", "");
//                if (featureNameCn instanceof JSONNull) {
//                    featureNameCn = "";
//                }
//                Object featureValueString = param.getOrDefault("featureValueString", "");
//                if (featureValueString instanceof JSONNull) {
//                    featureValueString = getByJsonObj(param,"featureValue") + getByJsonObj(param,"featureUnit");
//                }
//                sb.append((String)featureNameCn + ": "+ (String)featureValueString + "\n");
//            }
//            productEntity.setAttributes(sb.toString());
//            //产品OEM号码
//            JSONArray partOriginFactoryList = (JSONArray) partDTO.get("partOriginFactoryList");
//            List<ProductRefEntity> oemList = new ArrayList<>();
//            for (Object oemObj : partOriginFactoryList) {
//                JSONObject oem = (JSONObject) oemObj;
//                String brand = (String)oem.get("factoryBrand");
//                JSONArray oems = (JSONArray)oem.get("factoryNames");
//                for (Object o : oems) {
//                    ProductRefEntity productRefEntity = new ProductRefEntity();
//                    productRefEntity.setRefNo((String) o);
//                    productRefEntity.setBrand(brand);
//                    productRefEntity.setProductNo(partNo);
//                    oemList.add(productRefEntity);
//                }
//            }
//            System.out.println(productEntity);
//            refWriter.write(oemList,false);
//            productWriter.writeRow(productEntity,false);
//            productWriter.flush();
//            refWriter.flush();
//            Thread.sleep(2000);
//        }
//    }
//
//    public String getByJsonObj(JSONObject obj,String key) {
//        Object o = obj.get(key);
//        if (o instanceof JSONNull) {
//            return "";
//        } else {
//            if (StrUtil.isBlank((String)o)) {
//                return "";
//            }
//            return (String)o;
//        }
//    }
//
//}
